//
//  FeaturedSongsViewController.m
//  Mecca
//
//  Created by Nico Villanueva iOS Dev on 26/8/14.
//  Copyright (c) 2014 TapDash Heuristics. All rights reserved.
//

#import "FeaturedSongsViewController.h"

#import "Constant.h"

#import "PostClass.h"

#import "JsonClass.h"

#import "PostClass.h"

#import "ParserClass.h"

#import "FeaturedSongTableViewCell.h"

#import "StarRatingView.h"

#import "FeaturedSongs.h"

#import "UIManager.h"

#import "Songs.h"

#import "LocalPlayerClass.h"

#import "UserDetails.h"

@interface FeaturedSongsViewController (){
    
    NSDictionary *json;
 
}

@end

@implementation FeaturedSongsViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(somethingHappens:) name:@"cancelFeatureSong" object:nil]; // Register
    
    UserDetails *userDetails = [[ParserClass sharedItems] userDetails][0];
    
    self.receivedData = [NSMutableData data];
    NSURLRequest *request = [[PostClass sharedItems] getFeaturedSong:[UIDevice currentDevice].identifierForVendor.UUIDString andUserID:[userDetails idUser]];
    _theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self];
    
    [self.tableView setBackgroundColor:[UIColor colorWithRed:28.0/255.0 green:28.0/255.0 blue:28.0/255.0 alpha:1.0]];
  
}

#pragma mark - New Fetch Data;

-(void) somethingHappens:(NSNotification*) notification
{
    NSLog(@"notification = %@",[notification object]);
    
    [self.theConnection cancel];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    
    [_receivedData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"Error data = %@", error);
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    
    
    if (connection == self.theConnection) {
        
        [_receivedData appendData:data];
        
        //  NSLog(@"data parse = %@", _receivedData);
        
    }
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
    
    if (connection == self.theConnection) {
        
        if (!(_receivedData == nil)) {
            
            json = [JsonClass jsonParser:_receivedData];
            
            [[ParserClass sharedItems] songDetails:json];
            
            
            [self.tableView reloadData];
            
            NSLog(@"Featured Song = %@", json);
            
            NSLog(@"Datas = %i",[[[ParserClass sharedItems] songDetails] count]);
            
            FeaturedSongs *featuredSong = [[ParserClass sharedItems] songDetails][0];
            
            self.lblFeatSong.text = [featuredSong songName];
            
        }
        
        
        NSLog(@"Finish");
    }
    
}



#pragma mark - Other Outlets;


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)btnBack:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark Table Delegate


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    //#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return [[[ParserClass sharedItems] songDetails] count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Configure the cell...
    
    static NSString *cellIdentifier = @"Cell";
    
    FeaturedSongTableViewCell *cell = (FeaturedSongTableViewCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    
    if (cell == nil) {
        
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"FeaturedSongTableViewCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
    
    UIView *selectionColor = [[UIView alloc] init];
    selectionColor.backgroundColor = [UIColor colorWithRed:(38/255.0) green:(38/255.0) blue:(38/255.0) alpha:1];
    cell.selectedBackgroundView = selectionColor;
    
    Songs *featuredSong = [[ParserClass sharedItems] songDetails][indexPath.row];
    
    
    cell.lblSongName.text = [featuredSong songName];
    cell.lblDuration.text = [featuredSong durations];
    
    
    int likes = [[featuredSong ratings] intValue];
    
    StarRatingView* starViewNoLabel = [[StarRatingView alloc]initWithFrame:CGRectMake(215, 19, 45, 8) andRating:likes withLabel:NO animated:YES];
    
    [starViewNoLabel setUserInteractionEnabled:NO];
    
    [cell.customCell addSubview:starViewNoLabel];

    
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LocalPlayerClass *lp = [LocalPlayerClass sharedItems];
    
    [lp.audioPlayer stop];
    
    
    
    Songs *song = [[ParserClass sharedItems] songDetails][indexPath.row];
    
    [[ParserClass sharedItems] getUrlSong:[NSString stringWithFormat:@"http://192.185.93.102/~boomeran/backend-panel/%@", [song songLocation]] andCount:indexPath.row andFileSize:[song fileSize] andSongPreview:[NSString stringWithFormat:@"http://192.185.93.102/~boomeran/backend-panel/%@", [song songPreview]]];
    
    [[ParserClass sharedItems] getType:@"Songs"];
    
     [[ParserClass sharedItems] getImgStr:[[[json objectForKey:@"Songs"] objectAtIndex:indexPath.row] objectForKey:@"Album Image"]];
    
    [[ParserClass sharedItems] getAlbumID:[[[json objectForKey:@"Songs"] objectAtIndex:indexPath.row] objectForKey:@"Album ID"]];
    
    [[ParserClass sharedItems] getArtistName:[[[json objectForKey:@"Songs"] objectAtIndex:indexPath.row] objectForKey:@"Artist Name"]];
    
    [self.navigationController pushViewController:[[UIManager sharedItems] playSong] animated:YES];
    
    NSLog(@"Likes Data: %@",[song likes]);
    
    
    
}

@end
